import os.path

import numpy as np
import torch


def construct_rules(path):
    rules = []
    # no constraints on API parameters
    if not os.path.exists(path):
        return rules

    with open(path, 'r', encoding='utf-8') as f:
        for line in f.readlines():
            line = line.strip()
            rule = f'lambda d: not {line}'
            rules.append(eval(rule))
    return rules


def generate_tensor(shape, dtype, dtype_range):
    dtype = eval(dtype)
    dtype_min = dtype_range['min']
    dtype_max = dtype_range['max']

    # generate tensor range(can be configured)
    t = torch.tensor(np.random.uniform(dtype_min, dtype_max, shape), dtype=dtype)
    return t
